package persistance;

import namespaces.NamespaceConstants;
import thewebsemantic.Bean2RDF;
import thewebsemantic.RDF2Bean;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;

public class RDFModel {

	Model rdfGrapg;
	Bean2RDF writer;
	RDF2Bean reader;

	private static RDFModel rdfModel;

	/** Creating model and setting it for different namespace prefixes */

	private RDFModel() {
		rdfGrapg = ModelFactory.createDefaultModel();
		rdfGrapg.setNsPrefix("schema", NamespaceConstants.SCHEMA);
		rdfGrapg.setNsPrefix("xsd", NamespaceConstants.XSD);
		rdfGrapg.setNsPrefix("dc", NamespaceConstants.DC);

		/** We allow these variables to write to or read from rdf graph */

		writer = new Bean2RDF(rdfGrapg);
		reader = new RDF2Bean(rdfGrapg);
	}

	public static RDFModel getModel() {
		if (rdfModel == null) {
			rdfModel = new RDFModel();
		}
		return rdfModel;
	}

	/** Saving object to RDF model */

	public void save(Object o) {
		writer.save(o);
	}

	/** Reading from RDF model */

	public Object load(String url) {
		return reader.load(url);
	}

	public void print() {
		rdfGrapg.write(System.out, "TURTLE");
	}
}
